package oldVersion.storage;
/*need JMF: http://java.sun.com/products/java-media/jmf/index.jsp */
/*
 * MPlayer.java
 *
 * Created on February 14, 2009, 10:08 AM
 */



import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author  Lei Gao
 */
public class MPlayer extends javax.swing.JFrame implements ActionListener, Runnable{
    private Player player;
    private File file;
    private JFileChooser ch;
    private JFrame frame;

    /** Creates new form MPlayer */
    public MPlayer() {
        initLookAndFeel();
        initComponents();
        init();
    }
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        FileMenu = new javax.swing.JMenu();
        OpenFile = new javax.swing.JMenuItem();
        OpenRTP = new javax.swing.JMenuItem();
        EditMenu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panel.setLayout(new java.awt.BorderLayout());

        FileMenu.setText("File");

        OpenFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, 0));
        OpenFile.setText("Open  File");
        OpenFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenFileActionPerformed(evt);
            }
        });
        FileMenu.add(OpenFile);

        OpenRTP.setText("Open RTP");
        OpenRTP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenRTPActionPerformed(evt);
            }
        });
        FileMenu.add(OpenRTP);

        jMenuBar1.add(FileMenu);

        EditMenu.setText("Edit");
        jMenuBar1.add(EditMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 237, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 22, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void init(){
        ch=new JFileChooser();
        frame=this;
        //ImageIcon icon= new ImageIcon("D:\\Projects\\Practicum\\GaoleiMP3\\src\\player\\image\\images.jpg");
        ImageIcon icon= new ImageIcon("./src/player/image/images.jpg");
        JLabel label= new JLabel(icon);
        panel.add(label);
        frame.pack();
    }
    
    private void initLookAndFeel() {
        String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        try {
            UIManager.setLookAndFeel(plaf);
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            
        }
    }
    
    private void createPlayer(MediaLocator mrl){
        try {
            closePreviousPlayer();
            player = Manager.createPlayer(mrl);
            player.addControllerListener(new ControllerHand());
            player.start();
            if(file!=null)
            setTitle(file.getName());
            //this.validate();
        } catch (IOException ex) {
            Logger.getLogger(MPlayer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoPlayerException ex) {
            Logger.getLogger(MPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    private void closePreviousPlayer(){
        if(player==null)
            return;
        player.stop();
        player.deallocate();
        file=null;
        Component visual=player.getVisualComponent();
        Component control=player.getControlPanelComponent();
        if(visual!=null)
            panel.remove(visual);
        if(control!=null)
            panel.remove(control);
        this.validate();

    }    
    
private void OpenFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenFileActionPerformed
// TODO add your handling code here:
    ch.setDialogTitle("Please Open a file");
    ch.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int returnVal = ch.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file=ch.getSelectedFile();
            try {
                createPlayer(new MediaLocator(file.toURI().toURL()));
            } catch (MalformedURLException ex) {
                Logger.getLogger(MPlayer.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    else file=null;
    
}//GEN-LAST:event_OpenFileActionPerformed

private void OpenRTPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenRTPActionPerformed
// TODO add your handling code here:
    
    String url="http://ussproject.googlecode.com/svn/trunk/src/mp3s/Satelight.mp3";
    createPlayer(new MediaLocator(url));
}//GEN-LAST:event_OpenRTPActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MPlayer().setVisible(true);
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu EditMenu;
    private javax.swing.JMenu FileMenu;
    private javax.swing.JMenuItem OpenFile;
    private javax.swing.JMenuItem OpenRTP;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel panel;
    // End of variables declaration//GEN-END:variables

    
    private class ControllerHand implements ControllerListener {

        public void controllerUpdate(ControllerEvent e) {
            if (e instanceof RealizeCompleteEvent) {
                Component visual = player.getVisualComponent();
                if (visual != null) {
                    panel.removeAll();
                    panel.add(visual, BorderLayout.CENTER);
                } else {
                    //return;
                }
                Component control = player.getControlPanelComponent();
                if (control != null) {
                    panel.removeAll();
                    panel.add(control,BorderLayout.SOUTH);
                    //frame.remove(panel);
                    //frame.add(control,BorderLayout.SOUTH);
                    frame.pack();

                }
                panel.doLayout();
                return;
            }

        }

    }
}
